package com.dwxt.stadium.service;

import com.dwxt.common.base.BaseResult;
import com.dwxt.common.util.DateUtils;
import com.dwxt.stadium.dao.PayOrderMapper;
import com.dwxt.stadium.entity.PayOrderExample;
import org.apache.commons.collections.OrderedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GraphService {

    @Autowired
    private PayOrderMapper payOrderMapper;

    public BaseResult stadiumGraph() {
        List<String> xList = new ArrayList<>();
        xList.add("足球");
        xList.add("羽毛球");
        xList.add("乒乓球");
        xList.add("游泳");
        xList.add("篮球");

        List<Long> yList = new ArrayList<>();
        for (String s : xList) {
            PayOrderExample example = new PayOrderExample();
            example.createCriteria().andDetailLike("%" + s + "%");
            long count = payOrderMapper.countByExample(example);
            yList.add(count);
        }
        Map map = new HashMap();
        map.put("xArr", xList);
        map.put("yArr", yList);
        return BaseResult.ok(map);
    }

    public BaseResult stadiumOrderMonthGraph() {
        int year = DateUtils.getYear(null);
        int month = DateUtils.getMonth(null);
        List<Integer> xList = new ArrayList<>();
        List countList = new ArrayList<>();
        List amountList = new ArrayList<>();
        for (int i = month + 1, count = 0; count < 12; count++, i++) {
            int x;
            if (i > 12) {
                x = i - 12;
            } else {
                x = i;
            }
            xList.add(x);
            String date;
            if (x > month) {
                if (x < 10) {
                    date = year - 1 + "-0" + x;
                } else {
                    date = year - 1 + "-" + x;
                }
            } else {
                if (x < 10) {
                    date = year + "-0" + x;
                } else {
                    date = year + "-" + x;
                }
            }


            //System.out.println(date);
            Map map = payOrderMapper.stadiumOrderMonthGraph(date);

            countList.add(map.get("count"));
            amountList.add(map.get("amount"));
        }
        Map countMap = new HashMap();
        countMap.put("name", "订单数量");
        countMap.put("data", countList);

        Map amountMap = new HashMap();
        amountMap.put("name", "订单金额");
        amountMap.put("data", amountList);

        List yList = new ArrayList();
        yList.add(countMap);
        yList.add(amountMap);

        Map graph = new HashMap();
        graph.put("xList", xList);
        graph.put("yList", yList);

        return BaseResult.ok(graph);
    }
}
